package Demo1;

import java.util.Arrays;
import java.util.Comparator;

//创建两个学生对象，进行排序
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {

        return this.age - o.age;
    }
}

public class test {
    public static void mysort(Comparable[] comparables){
        for (int i = 0; i < comparables.length -1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    static class AgeComparator implements Comparator<Student>{
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age - o2.age;
        }
    }

    static class NameComparator implements Comparator<Student>{
        @Override
        public int compare(Student o1, Student o2) {
            return o1.name.compareTo(o2.name);
        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",22);
        Student student2 = new Student("lisi",38);
        AgeComparator age = new AgeComparator();
        System.out.println(age.compare(student1, student2));
        System.out.println("==============");
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",22);
        students[2] = new Student("wangwu",59);
        AgeComparator ageComparator1 = new AgeComparator();
        Arrays.sort(students,ageComparator1);//Array.Sort方法中可以添加排序方法参数
        System.out.println(Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        System.out.println(Arrays.toString(students));


    }
    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",10);
        students[1] = new Student("lisi",22);
        students[2] = new Student("wangwu",59);
        //Arrays.sort(students);
        mysort(students);
        System.out.println(Arrays.toString(students));
    }

    /*public static void main1(String[] args) {
        Student stu1 = new Student("zhangsan",10);
        Student stu2 = new Student("lisi",22);
        if(stu1.compareTo(stu2)>0){
            System.out.println("stu1>stu2");
        }
        else{
            System.out.println("stu1<=stu2");
        }
        String str1 = "zhangsan";
        String str2 = "lisi";
        if(str1.compareTo(str2)>0){
            System.out.println("str1>str2");
        }
        else{
            System.out.println("str1<=str2");
        }
    }*/
}
